package tw.asap.plus.ui.custom;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;
import tw.asap.plus.utils.ScreenUtil;

/**
 * Created by yamlee on 15/8/14.
 */
public class AppBar extends FrameLayout {
    @InjectView(R.id.tv_navigation_header_left)
    TextView tvNavigationHeaderLeft;
    @InjectView(R.id.iv_navigation_header_left)
    ImageView ivNavigationHeaderLeft;
    @InjectView(R.id.tv_naviation_title)
    TextView tvNaviationTitle;
    @InjectView(R.id.tv_navigation_header_right)
    TextView tvNavigationHeaderRight;
    @InjectView(R.id.iv_navigation_header_right)
    ImageView ivNavigationHeaderRight;
    @InjectView(R.id.layout_navigation_header)
    RelativeLayout layoutNavigationHeader;
    @InjectView(R.id.rl_left_rect)
    RelativeLayout rlLeftRect;
    @InjectView(R.id.rl_right_rect)
    RelativeLayout rlRightRect;

    private Context mContext;

    public static final int NAVIGATION_LEFT = 0X00;
    public static final int NAVIGATION_RIGHT = 0X01;

    public AppBar(Context mContext) {
        super(mContext);
        initView(mContext);
    }

    public AppBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppBar);
        String mText = a.getString(R.styleable.AppBar_appbar_title);
        int left_img_id = a.getResourceId(R.styleable.AppBar_left_img, 0);
        int right_img_id = a.getResourceId(R.styleable.AppBar_right_img, 0);
        float textSize = a.getDimension(R.styleable.AppBar_title_size, ScreenUtil.dip2px(context, 10));
        int background = a.getResourceId(R.styleable.AppBar_appbar_background, 0);
        int textColor = a.getResourceId(R.styleable.AppBar_title_color, R.color.palette_white);
        a.recycle();

        this.mContext = context;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.include_layout_heaer, this);
        ButterKnife.inject(view);

        if (left_img_id != 0) {
            ivNavigationHeaderLeft.setVisibility(VISIBLE);
            ivNavigationHeaderLeft.setImageResource(left_img_id);
        }

        if (right_img_id != 0) {
            ivNavigationHeaderRight.setVisibility(VISIBLE);
            ivNavigationHeaderRight.setImageResource(right_img_id);
        }

        if (!TextUtils.isEmpty(mText)) {
            tvNaviationTitle.setText(mText);
            tvNaviationTitle.setTextColor(context.getResources().getColor(textColor));
        }

        if (background != 0) {
            layoutNavigationHeader.setBackgroundColor(getResources().getColor(background));
        }


    }

    public void setRightImage(int id){
        ivNavigationHeaderRight.setImageResource(id);
    }

    public AppBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void setBackgroundColor(int color) {
        layoutNavigationHeader.setBackgroundColor(color);
    }

    public void setRightTitle(String rightTitle, OnClickListener onClickListener) {
        tvNavigationHeaderRight.setText(rightTitle);
        tvNavigationHeaderRight.setOnClickListener(onClickListener);
    }

    private void initView(Context context) {
        this.mContext = context;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.include_layout_heaer, this);
        ButterKnife.inject(view);
    }

    public void setLeftClickEvent(OnClickListener listener) {
        rlLeftRect.setOnClickListener(listener);
    }

    public void setRightClickEvent(OnClickListener listener) {
        rlRightRect.setOnClickListener(listener);
    }


    public void setLeftNavigation(String leftTitle, final OnLeftClickListener onLeftClickListener) {
        tvNavigationHeaderLeft.setVisibility(View.VISIBLE);
        ivNavigationHeaderLeft.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(leftTitle)) {
            tvNavigationHeaderLeft.setText(leftTitle);
        }
        tvNavigationHeaderLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onLeftClickListener.onClick(v);
            }
        });
    }

    public void setLeftNavigation(int imgResId, final OnLeftClickListener onLeftClickListener) throws NotFoundException {
        tvNavigationHeaderLeft.setVisibility(View.GONE);
        ivNavigationHeaderLeft.setVisibility(View.VISIBLE);
        if (imgResId > 0) {
            Drawable drawable = mContext.getResources().getDrawable(imgResId);
            if (null == drawable)
                throw new NotFoundException("thre resoruce id not found in drawable");
            ivNavigationHeaderLeft.setImageResource(imgResId);
        }
        ivNavigationHeaderLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onLeftClickListener.onClick(v);
            }
        });
    }


    public void setNavigationWidth(int which, int width) {
        ImageView view = null;
        switch (which) {
            case NAVIGATION_LEFT:
                view = ivNavigationHeaderLeft;
                break;
            case NAVIGATION_RIGHT:
                view = ivNavigationHeaderRight;
                break;
        }
        assert view != null;
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
        params.height = ScreenUtil.dip2px(getContext(), width);
        params.width = ScreenUtil.dip2px(getContext(), width);
        view.setLayoutParams(params);
    }

    public void setRightNavigation(String rightTitle, final OnRightClickListener onRightClickListener) {
        tvNavigationHeaderRight.setVisibility(View.VISIBLE);
        tvNavigationHeaderRight.setText(rightTitle);
        tvNavigationHeaderRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onRightClickListener.onClick(v);
            }
        });
    }

    public void setRightNavigation(int imgResId, final OnRightClickListener onRightClickListener) throws NotFoundException {
        Drawable drawable = mContext.getResources().getDrawable(imgResId);
        if (null == drawable) throw new NotFoundException("thre resoruce id not found in drawable");
        ivNavigationHeaderRight.setVisibility(View.VISIBLE);
        ivNavigationHeaderRight.setImageResource(imgResId);
        ivNavigationHeaderRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onRightClickListener.onClick(v);
            }
        });
    }


    public void setShowLeft(boolean showLeft) {
        if (showLeft) {
            tvNavigationHeaderLeft.setVisibility(View.VISIBLE);
            ivNavigationHeaderLeft.setVisibility(View.VISIBLE);
        } else {
            tvNavigationHeaderLeft.setVisibility(View.GONE);
            ivNavigationHeaderLeft.setVisibility(View.GONE);
        }
    }

    public void setShowRight(boolean showRight) {
        if (showRight) {
            tvNavigationHeaderRight.setVisibility(View.VISIBLE);
            ivNavigationHeaderRight.setVisibility(View.VISIBLE);
        } else {
            tvNavigationHeaderRight.setVisibility(View.GONE);
            ivNavigationHeaderRight.setVisibility(View.GONE);
        }
    }

    public void setTitle(String title) {
        tvNaviationTitle.setText(title);
    }

    public interface OnLeftClickListener {
        void onClick(View v);
    }

    public interface OnRightClickListener {
        void onClick(View v);
    }


}
